#pragma once
#include "ModelEntity.h"
#include "GLState.h"

class ZhicheEntity : public ModelEntity {
public:
	static ZhicheEntity* New();

	~ZhicheEntity();

	void slotCameraChange();

	void updateState() override;

	RenderOrder renderOrder() const override { return RenderOrder::ZhicheAsLast; }

	//@note Main-GUI-Thread(should avoid high-concurrency change of door state switch(open/close))
	void handleDoor(HDir side, Switch status);

	void resetDoor() {
		handleDoor(HDir::LEFT, Switch::OFF);
		handleDoor(HDir::RIGHT, Switch::OFF);
	}

	/*****************************************************************************/
	// called per frame
	void renderAsTexture(FBO* fboToWrite);

	static bool hasRenderedDone() { return s_hasRenderedDone; }

private:
	ZhicheEntity();

	void loadAsyncDoors();

	FBO* m_fbo = nullptr;
	Shader m_shader;

	CVector<Entity*> m_doorLeftModels, m_doorRightModels;
	Vec3 m_doorLeftMinZPoint, m_doorRightMinZPoint;
	CMap<HDir, Switch> m_doorStatusMap;
	bool m_isDoorsLoadedDone = false;
	bool m_isCameraChanged = false;
	bool m_isDoorChanged = false;

	static bool s_hasRenderedDone;
};